$(function() {
	var filter = {};
	var filtertxt1 = "需要带：";
	var filtertxt2 = "不能带：";
	var filtertxt3 = "谁和谁不能在一起：";
	var filtertxt4 = "第几位出现：";

	function refresh() {
		$('div.blind').removeClass('none');
		setTimeout(function() {
			$('div.blind').addClass('none');
		},300);
	} 
	function rm(id) {
		delete filter[id];
/*
        $.ajax({
            url: _url,
            dataType: 'html',
            type: 'POST',
            data: {
            },
            beforeSend: function(){
            },
            error: function(xhr, type, exception){
            },
            success: function(data){
                $('div.loading-background').css('z-index','0')
                $('div.load-background').after(data)
                $('div#main').css('opacity','1').addClass('show');
            }
        });
*/
		$('div.blind').removeClass('none');
		setTimeout(function() {
			$('div.blind').addClass('none');
		},300);
//		alert(JSON.stringify(filter));
	}

	$(document).on('click', 'div.plus-filter', function() {
		if($('div.filter-edit').html() != '') return;
		var id = $(this).attr('id');
		switch (id) {
			case "filter1":
				var span = $('<span>').addClass('filtername').html(filtertxt1);
				var input = $("<input type='text'></input>").addClass('one');
				var div = $('<div>').addClass('ok').html("确定");
				$('div.filter-edit').attr('id','filter1').append(span, input, div);
				break;
			case "filter2":
				var span = $('<span>').addClass('filtername').html(filtertxt2);
				var input = $("<input type='text'></input>").addClass('one');
				var div = $('<div>').addClass('ok').html("确定");
				$('div.filter-edit').attr('id','filter2').append(span, input, div);
				break;
			case "filter3":
				var span = $('<span>').addClass('filtername').html(filtertxt3);
				var input1 = $("<input type='text'></input>").addClass('two').attr('id','1');
				var input2 = $("<input type='text'></input>").addClass('two').attr('id','2');
				var div = $('<div>').addClass('ok').html("确定");
				$('div.filter-edit').attr('id','filter3').append(span, input1, input2, div);
				break;
			case "filter4":
				var span1 = $('<span>').addClass('filtername').html(filtertxt4);
				var span2 = $('<span>').addClass('filtername').html("第");
				var input1 = $("<input type='text'></input>").addClass('two').attr('id','1');
				var span3 = $('<span>').addClass('filtername').html("位出现");
				var input2 = $("<input type='text'></input>").addClass('two').attr('id','2');
				var div = $('<div>').addClass('ok').html("确定");
				$('div.filter-edit').attr('id','filter4').append(span1, span2, input1, span3, input2, div);
				break;
		}
	})
	$(document).on('click', 'div.ok', function() {
		var ul = $('.filtered-item ul');
		var that = $(this);
		switch (that.parent('.filter-edit').attr('id')) {
			case "filter1":
				str = that.siblings('input').val();
				filter.with = str;
				var li = $('<li></li>');
				var x = $('<span class="del-filter">x</span>');
				var name = $('<span>').addClass('filtername').html(filtertxt1);
				var content = $('<span>').addClass('filtername').html(str);
				li.attr('id','with').append(x, name, content);
				ul.append(li);
				break;
			case "filter2":
				str = that.siblings('input').val();
				filter.notwith = str;
				var li = $('<li></li>');
				var x = $('<span class="del-filter">x</span>');
				var name = $('<span>').addClass('filtername').html(filtertxt2);
				var content = $('<span>').addClass('filtername').html(str);
				li.attr('id','notwith').append(x, name, content);
				ul.append(li);
				break;
			case "filter3":
				str1 = that.siblings("input[id='1']").val();
				str2 = that.siblings("input[id='2']").val();
				filter.with2 = {'first':str1, 'second':str2}; 
				var li = $('<li></li>');
				var x = $('<span class="del-filter">x</span>');
				var name = $('<span>').addClass('filtername').html(filtertxt3);
				var content = $('<span>').addClass('filtername').html(str1+"和"+str2);
				li.attr('id','with2').append(x, name, content);
				ul.append(li);
				break;
			case "filter4":
				str1 = that.siblings("input[id='1']").val();
				str2 = that.siblings("input[id='2']").val();
				filter.poswith = {'pos':str1, 'value':str2}; 
				var li = $('<li></li>');
				var x = $('<span class="del-filter">x</span>');
				var name = $('<span>').addClass('filtername').html(filtertxt4);
				var content = $('<span>').addClass('filtername').html("第"+str1+"位出现"+str2);
				li.attr('id','poswith').append(x, name, content);
				ul.append(li);
				break;
		}
		$('div.filter-edit').html('');
		refresh();
	})
	$(document).on('click', 'span.del-filter', function() {
		var id = $(this).parent('li').attr('id');
		$(this).parents('li').remove();
		rm(id);
	})
});
